<?php

class FAjaxRequest
{
 
    private $model;
    private $function;
    private $action;
    
    public function __construct($model, $function, $action)
    {
        $this->setModel($model);
        $this->setFunction($function);
        $this->setAction($action);
    }

    public function generate()
    {               
        return "
            Ext.Ajax.request({
                url: 'src/php/handler.php',
                params: {
                    action: 'submit',
                    function: '{$this->getFunction()}',
                    manager: '{$this->getModel()}'
                },
                success: function(response) {
                    obj = Ext.JSON.decode(response.responseText);

                    if ( obj.confirmOnClose )
                    {                                            
                        Ext.Msg.confirm('Confirmação', '<b>' + obj.onCloseConfirmMsg + '</b>', function(btn)
                        {
                            if( btn == 'yes' )
                            {
                                {$this->getAction()}
                            }
                        }, this);
                    }
                    else
                    {
                        if ( this.obj.showSuccessMsg )
                        {
                            Ext.create('FMessageSuccess', {
                                msg: this.obj.msg,
                                btnAction: function(){{$this->getAction()}}
                            }).show();
                        }
                        else
                        {
                            {$this->getAction()}
                        }
                    }
                }
            });
        ";
    }
    
    public function getModel() {
        return $this->model;
    }

    public function setModel($model) {
        $this->model = $model;
    }

    public function getFunction() {
        return $this->function;
    }

    public function setFunction($function) {
        $this->function = $function;
    }

    public function getAction() {
        return $this->action;
    }

    public function setAction($action) {
        $this->action = $action;
    }
    
}

?>
